using System;
using MediaPortal.GUI.Library;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using MediaPortal.Player;
using System.Collections.Generic;
//using MediaPortal.Utils.Services;
using MediaPortal.GUI.View ;
using MediaPortal.Dialogs;
using System.Xml;
using System.Xml.XPath;
using System.ComponentModel;
using System.Threading;
using System.Web;



namespace OnlineVideos
{
    public class MetaCafeUtil:SiteUtilBase 
    {
    	public override string getSiteId()
		{
			return "5";
		}
        
        protected override String getUrl(String fsId)
        {            
            Log.Info("Getting metacafe video url for video:"+fsId);
            WebClient client = new WebClient();
            //String lsHtml = client.DownloadString(String.Format("http://www.metacafe.com/fplayer.php?itemID={0}&fs=n&t=embedded", fsId));
            String lsHtml = client.DownloadString((fsId));
            Regex loRegex = new Regex(@"mediaURL=(.*)&PostRoll",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled);
            String lsUrl = loRegex.Match(lsHtml).Groups[1].Value;
         
            lsUrl = HttpUtility.UrlDecode(lsUrl);
            lsUrl = lsUrl.Replace("gdaKey", "__gda__");
            lsUrl = lsUrl.Replace("&", "?");
            //lsUrl = lsUrl + "&txe=.flv";
            Log.Info("MetaCafe video flv url = {0}",lsUrl);
            return lsUrl; 
        }
        public override List<GUIOnlineVideos.VideoInfo> getVideoList(string fsUrl)
		{
			List<RssItem> loRssItemList = getRssDataItems(fsUrl);
			List<GUIOnlineVideos.VideoInfo> loVideoList = new List<GUIOnlineVideos.VideoInfo>();
			GUIOnlineVideos.VideoInfo video;
			foreach(RssItem rssItem in loRssItemList){
				video = new GUIOnlineVideos.VideoInfo();
				video.Description = rssItem.description;
				video.ImageUrl = rssItem.mediaThumbnail;
				video.Title = rssItem.title;
				video.Length = rssItem.contentList[0].duration;
				//video.VideoUrl = Regex.Match(rssItem.link,@"watch/([\d]*)").Groups[1].Value;
                video.VideoUrl = rssItem.link;
						loVideoList.Add(video);
				
			}
			return loVideoList;
		}/*
        public List<GUIOnlineVideos.VideoInfo> getRssData(String fsUrl)
        {

            XmlDocument doc = new XmlDocument();
            //XPathDocument doc = new XPathDocument(fsUrl);

            doc.Load(XmlReader.Create(fsUrl));
            XmlNamespaceManager expr = new XmlNamespaceManager(doc.NameTable);
            expr.AddNamespace("media", "http://search.yahoo.com/mrss");

            //Create the root XmlNode and generate an XmlNodeList from it.
            XmlNode root = doc.SelectSingleNode("//rss/channel/item", expr);
            XmlNodeList nodeList;
            nodeList = root.SelectNodes("//rss/channel/item");
            //RssItem loRssItem = new RssItem();
            //loRssItem.
            //This loops through each node found in the nodeList and adds the 
            //Node data to the DataTable.
            XmlAttributeCollection ac;
            List<GUIOnlineVideos.VideoInfo> loRssItems = new List<GUIOnlineVideos.VideoInfo>();
            GUIOnlineVideos.VideoInfo loRssItem;
            //GUIListItem loListItem;
            foreach (XmlNode chileNode in nodeList)
            {
                loRssItem = new GUIOnlineVideos.VideoInfo();                

                XmlNode node = chileNode.SelectSingleNode("title");
                loRssItem.Title = node.InnerText;

                node = chileNode.SelectSingleNode("link");
                loRssItem.VideoUrl = node.InnerText;

                node = chileNode.SelectSingleNode("description");
                loRssItem.Description = node.InnerText;

                node = chileNode.SelectSingleNode("media:thumbnail", expr);
                if (node != null)
                {
                    ac = node.Attributes;
                    loRssItem.ImageUrl = ac["url"].InnerText;
                }


                //node = chileNode.SelectSingleNode("media:group/media:content[@type=\"video/x-flv\"]", expr);
                //if (node != null)
                //{
                //    ac = node.Attributes;
                //    loRssItem.videoUrl = ac["url"].InnerText;
                //}

                //Log.Write(loRssItem.ToString());
                //loListItem = new GUIListItem(loRssItem.title);
                Regex loRegex = new Regex("watch/([^/]+)");
                
                loRssItem.VideoUrl = loRegex.Match(loRssItem.VideoUrl).Groups[1].Value;
                //Log.Write("metaCafe video id={0}",loListItem.Path);
                loRssItems.Add(loRssItem);

                
            }
            return loRssItems;
        }
       */
     

    }
}
